var app = getApp();
Page({
  data: {
    s: 0,
    startX: 0,
    lefts: 0,
    current: 0,
    currents: 0,

    searchstatus: false,
    index: 0,
    show: false,
    show1: false,
    show2: false,
    selectCarId: "", //选中的车型
    selectcolorid: "", //选中的颜色
    selectcolorlist: [], //选中的颜色列
    selectcolorIndex: 0,
    selectcolorGropName: "", //选中的颜色名
    array: [],
    colorGroup: [],

  },
  touchstart: function (e) {
    this.setData({
      startX: e.changedTouches[0].clientX
    })
  },
  tous(e) {

    if (e.changedTouches[0].clientX < this.data.startX) {
      console.log(111)
      let w = this.data.s += 1
      if (w < 38) {
        this.setData({
          s: w
        })
        let left = this.data.s * 750
        this.setData({
          lefts: left
        })
      } else {
        let ws = 750 * 37
        this.setData({
          s: 0,
          lefts: 0
        })
      }


    } else {
      if (this.data.s > 0) {
        let w = this.data.s -= 1
        this.setData({
          s: w
        })
        let left = w * 750
        this.setData({
          lefts: left
        })
      } else {
        let ws = 750 * 37
        this.setData({
          s: 37,
          lefts: ws
        })
      }

    }


  },
  searchs(e) {
    var _this = this
    this.setData({
      searchstatus: true
    })

    var carid = this.data.selectCarId;
    var searchword = this.data.searchword
    wx.request({
      url: app.globalData.apiurl + 'car_colorServiceApi/list?carid=' + carid + "&colorName=" + searchword,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
     
        _this.setData({
          colorGroup: res.data.result.group,
          searchlist: res.data.result.search,
          searchstatus: false
        }, () => {
         
          _this.hidebox1()
          _this.showbox2()
        })

      }
    })

    

  },


  showbox() {
    this.setData({
      show: true
    })
  },
  showbox1() {
    this.setData({
      show1: true
    })
  },
  showbox2() {
    this.setData({
      show2: true
    })
  },
  stops() {

  },
  hidebox() {
    this.setData({
      show: false
    })
  },
  hidebox1() {
    this.setData({
      show1: false
    })
  },
  hidebox2() {
    this.setData({
      show2: false
    })
  },
  // 事件处理函数
  look() {
    var _this = this
    wx.navigateTo({
      url: '../picture/picture?colorid=' + _this.data.selectcolorid
    })
  },
  onLoad() {
    var _this = this
    wx.request({
      url: app.globalData.apiurl + 'car_carServiceApi/list',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        _this.setData({
          array: res.data.result,
        }, () => {
          //默认去第一个车的数据
          _this.selectCar(0);
        })
      }
    })
  },

  getcolorList: function (carid) {
    var _this = this
    wx.request({
      url: app.globalData.apiurl + 'car_colorServiceApi/list?carid=' + carid,
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        _this.setData({
          colorGroup: res.data.result.group
        }, () => {
          //默认选择第一组 第一个颜色
          _this.selectcolor(0, 0);
        })
      }
    })
  },


  onclickSearchitem: function (e) {
    var _this = this;
    var clickid = e.currentTarget.dataset.id
 
    this.hidebox2();
    //通过id查找grup
    var index = 0;

    this.data.colorGroup.forEach(element => {
      var subindex = 0;
      element.list.forEach(subitem => {
        if (subitem.id == clickid) {
       
          _this.selectcolor(index, subindex);
        }
        subindex++;
      });
      index++;
    });
  },
  //界面选择
  onselectcolor: function (e) {
    var index = e.currentTarget.dataset.index
    var subindex = e.currentTarget.dataset.subindex
    this.selectcolor(index, subindex)
  },
  //更换车型, 读取颜色列表
  bindPickerChange: function (e) {
    this.selectCar(e.detail.value)
  },
  onslidechange(e) {
    this.calcSwip(e.currentTarget.id)
    this.selectcolor(this.data.selectcolorIndex, e.currentTarget.id)
  },
  calcSwip: function (index) {
    let w = Number(index)
    if (this.data.selectcolorlist.length > 5) {
      var index = w < 2 ? w : w - 2;
      if (index > (this.data.selectcolorlist.length - 5)) {
        index = this.data.selectcolorlist.length - 5
      }
      this.setData({
        current: index,
        currents: w
      })
    } else {
      this.setData({
        currents: w
      })
    }
  },

  selectcolor: function (index, subindex) {
    if (this.data.colorGroup[index] != null) {
      var imageurl = this.data.colorGroup[index].list[subindex].imgurl
      var list = this.data.colorGroup[index].list
      var groupname = this.data.colorGroup[index].groupname
      var selectcolorid = this.data.colorGroup[index].list[subindex].id

      this.setData({
        selectcolorIndex: index,
        selectcolorGropName: groupname,
        selectcolorlist: list,
        showimageurl: imageurl,
        selectcolorid: selectcolorid,
        show: false
      })

      this.calcSwip(subindex)
    }

  },
  selectCar: function (index) {
    var _this = this
    var carid = this.data.array[index].id
    this.setData({
      index: index,
      selectCarId: carid
    }, () => {
      _this.getcolorList(carid)
    })
  },
  onShareAppMessage() {
   
  }

})